using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI;
using System.Windows.Forms;
using OutlookBarWorkspaceQuickstart.Common.Constants;

namespace OutlookBarWorkspaceQuickstart.Common.Services
{
    public class ToolStripService
    {
        private WorkItem _rootWorkItem;
        private WorkItem _currentWorkItem;

        [InjectionConstructor]
        public ToolStripService([ServiceDependency] WorkItem workItem)
        {
            _currentWorkItem = workItem;
            _rootWorkItem = _currentWorkItem.RootWorkItem;
        }

        public void RegisterToolStrip(ToolStrip toolstrip, string extensionSite)
        {
            UnregisterCurrentToolStrip(extensionSite);                     
            ToolStripItemCollection items = toolstrip.Items;
            RegisterToolStripItemCollection(extensionSite, items);
        }

        private void RegisterToolStripItemCollection(string extensionSite, ToolStripItemCollection items)
        {
            for (int i = 0, count = items.Count; i < count; i++)
            {

                ToolStripItem item = items[0];
                item.Owner = null;
                _rootWorkItem.UIExtensionSites[extensionSite].Add<ToolStripItem>(item);
                
                // register sites for ToolStripSplitButton, ToolStripComboBox, ToolStripDropDownButton, ToolStripLabel, ToolStripTextBox, ToolStripProgressBar
                // add commands for ToolStripSplitButton, ToolStripButton, ToolStripComboBox, ToolStripDropDownButton, ToolStripTextBox
                RegisterToolStripItem(item);
            }
        }

        private void RegisterToolStripItemCollection(ToolStripItemCollection items)
        {
            for (int i = 0, count = items.Count; i < count; i++)
            {

                ToolStripItem item = items[i];
                
                // register sites for ToolStripSplitButton, ToolStripComboBox, ToolStripDropDownButton, ToolStripLabel, ToolStripTextBox, ToolStripProgressBar
                // add commands for ToolStripSplitButton, ToolStripButton, ToolStripComboBox, ToolStripDropDownButton, ToolStripTextBox
                RegisterToolStripItem(item);
            }
        }

        private void RegisterToolStripItem(ToolStripItem item)
        {
            if (item.Tag != null && item.Tag is string)
            {
                string name = (string)item.Tag;
                if (item is ToolStripSplitButton || item is ToolStripComboBox
                  || item is ToolStripDropDownButton || item is ToolStripLabel
                  || item is ToolStripTextBox || item is ToolStripProgressBar)
                {
                    if (!_rootWorkItem.UIExtensionSites.Contains(name))
                    {
                        _rootWorkItem.UIExtensionSites.RegisterSite(name, item);
                    }
                }
                if (item is ToolStripButton)
                {
                    _currentWorkItem.Commands[name].AddInvoker(item, "Click");
                }
                if (item is ToolStripDropDownButton)
                {
                    _currentWorkItem.Commands[name].AddInvoker(item, "Click");
                    ToolStripDropDownButton dropdown = (ToolStripDropDownButton)item;
                    RegisterToolStripItemCollection(dropdown.DropDownItems);
                }
                if (item is ToolStripComboBox)
                {
                    _currentWorkItem.Commands[name].AddInvoker(item, "SelectedIndexChanged");                    
                }
                if (item is ToolStripTextBox)
                {
                    _currentWorkItem.Commands[name].AddInvoker(item, "TextChanged");
                }
                if (item is ToolStripSplitButton)
                {
                    _currentWorkItem.Commands[name].AddInvoker(item, "ButtonClick");
                    ToolStripSplitButton split = (ToolStripSplitButton)item;
                    if (split.DropDown != null && split.DropDown.Items.Count > 0)
                    {
                        RegisterToolStripItemCollection(split.DropDown.Items);
                    }
                }
                if (item is ToolStripMenuItem)
                {
                    _currentWorkItem.Commands[name].AddInvoker(item, "Click");
                    ToolStripMenuItem menu = (ToolStripMenuItem)item;
                    if (menu.DropDown != null && menu.DropDown.Items.Count > 0)
                    {
                        RegisterToolStripItemCollection(menu.DropDown.Items);
                    }
                }
            }
        }

        private void UnregisterCurrentToolStrip(string extensionSite)
        {
            if (_rootWorkItem.UIExtensionSites[extensionSite].Count > 0)
            {
                ToolStripItem dummy = _rootWorkItem.UIExtensionSites[extensionSite].Add<ToolStripItem>(new ToolStripButton());
                ToolStripItemCollection items = dummy.Owner.Items;
                foreach (ToolStripItem item in items)
                {
                    // unregister sites for ToolStripSplitButton, ToolStripComboBox, ToolStripDropDownButton, ToolStripLabel, ToolStripTextBox, ToolStripProgressBar
                    if (item.Tag != null && item.Tag is string)
                    {
                        string name = (string)item.Tag;
                        if (item is ToolStripSplitButton || item is ToolStripComboBox
                          || item is ToolStripDropDownButton || item is ToolStripLabel
                          || item is ToolStripTextBox || item is ToolStripProgressBar)
                        {
                            if (_rootWorkItem.UIExtensionSites.Contains(name))
                            {
                                _rootWorkItem.UIExtensionSites.UnregisterSite(name);
                            }
                        }
                    }
                }
                _rootWorkItem.UIExtensionSites[extensionSite].Clear();
            }
        }
    }
}
